<template>
  <div>
    <div v-if="!query.dictName">
      <div class="my-code">←点击左侧列表中字典查看详情</div>
    </div>
    <div v-else>
      <!--工具栏-->
      <div class="head-container">
        <div v-if="searchToggle">
          <!-- 搜索 -->
          <el-input v-model="query.label" clearable size="small" placeholder="输入字典标签查询" style="width: 200px;" class="filter-item" @keyup.enter="fetchList" />
          <rrOperation />
        </div>
      </div>
      <!--表格渲染-->
      <el-table ref="table" v-loading="tableLoading" :data="tableData" highlight-current-row style="width: 100%;" @selection-change="selectionChangeHandler">
        <el-table-column label="所属字典">
          {{ query.dictName }}
        </el-table-column>
        <el-table-column prop="label" label="字典标签" />
        <el-table-column prop="value" label="字典值" />
        <el-table-column prop="dictSort" label="排序" />
        <el-table-column v-permission="[].concat(permission.edit,permission.remove)" label="操作" width="130px" align="center" fixed="right">
          <template v-slot="{row}">
            <ud-operation :data="row"></ud-operation>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />

      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" v-model="dialog.visible" :title="dialog.title" width="500px">
        <el-form ref="formRef" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="字典标签" prop="label">
            <el-input v-model="form.label" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="字典值" prop="value">
            <el-input v-model="form.value" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="排序" prop="dictSort">
            <el-input-number v-model.number="form.dictSort" :min="0" :max="999" controls-position="right" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <template v-slot:footer class="dialog-footer">
          <el-button @click="dialog.visible = false">关闭</el-button>
          <el-button :loading="dialog.status === DIALOG_STATUS.PROCESSING" type="primary" @click="submitCE">确认</el-button>
        </template>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import udOperation from '@crud/UD.operation'

import dictDetailApi from '@/api/system/dictDetail'
import useCrud from '@crud/useCrud'
import { ref } from 'vue'

export default {
  components: { pagination, rrOperation, udOperation },
  setup() {
    const dictId = ref(0)
    const self = ref(null)
    let dictName = null
    return {
      self,
      dictId,
      dictName,
      permission: {
        create: ['dict:add'],
        edit: ['dict:edit'],
        remove: ['dict:del']
      },
      ...useCrud({
        title: "字典详情",
        defaultForm: { id: null, label: null, value: null, dictSort: 999, dictId: dictId },
        methods: {...dictDetailApi},
        hook: {
          beforeResetQuery: () => {
            dictName = self.value.query.dictName
          },
          afterResetQuery: () => {
            self.value.query.dictName = dictName
          }
        }
      })
    }
  },
  created() {
    this.self = this
  },
  data() {
    return {
      rules: {
        label: [
          { required: false, message: '请输入字典标签', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '请输入字典值', trigger: 'blur' }
        ],
        dictSort: [
          { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
        ]
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
 ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
